import { AIImage, MessageList, AIhttp } from "./AIUtils";
import { executeSQL, ExePython } from "../../api/AICode";
import { reactive } from "vue";
const myReq = reactive({
  ReqData: {
    sql: "",
    userID: 1
  }
});
const dataTools = [
  {
    type: "function",
    function: {
      name: "ExeSQL",
      description: "查询数据表，生成SQL语句",
      parameters: {
        type: "object",
        properties: {
          SQL: {
            description: "SQL语句",
            type: "string"
          }
        },
        required: ["SQL"]
      }
    }
  },

  {
    type: "function",
    function: {
      name: "drawChart",
      description: "生成统计图，生成统计图表，执行上述代码",
      parameters: {
        type: "object",
        properties: {
          charCode: {
            description: "python代码",
            type: "string"
          }
        },
        required: ["charCode"]
      }
    }
  }
];

const webTools = [
  // {
  //   type: "web_search"
  // }
];
const Knotools = [
  {
    type: "retrieval",
    retrieval: {
      knowledge_id: "1805804264640204800",
      prompt_template:
        '从文档\n"""\n{{knowledge}}\n"""\n中找问题\n"""\n{{question}}\n"""\n的答案，找到答案就仅使用文档语句回答问题，找不到答案就用自身知识回答并且告诉用户该信息不是来自文档。\n不要复述问题，直接开始回答。'
    }
  }
];

const drawTools = [
  {
    type: "function",
    function: {
      name: "drawImage",
      description: "用户需要绘图或画画时调用这个函数，并将图片显示出来",
      parameters: {
        type: "object",
        properties: {
          prompt: {
            description: "绘画的提示词",
            type: "string"
          }
        }
        // required: ["departure", "destination", "date"]
      }
    }
  }
];

export function SysFunction() {
  async function callFunction(
    functinName: string,
    properties: string,
    tools?: any
  ) {
    let proper;
    let result;
    let rs_ok;
    //模型返回调用函数
    switch (functinName) {
      case "ExeSQL":
        proper = JSON.parse(properties);
        result = await ExeSQL(proper.SQL);
        //模拟函数调用，并返回结果

        MessageList.push({
          role: "tool",
          content: result,
          tool_call_id: tools.id
        });

        AIhttp(MessageList);
        break;

      case "drawImage":
        console.log("drawImage");
        proper = JSON.parse(properties);
        console.log(proper.prompt);

        AIImage(proper.prompt, tools);
        break;

      case "drawChart":
        console.log("drawChart");
        proper = JSON.parse(properties);
        console.log(proper.charCode);
        rs_ok = await drawChart(proper.charCode);
        //模拟函数调用，并返回结果
        MessageList.push({
          role: "tool",
          content: rs_ok,
          tool_call_id: tools.id
        });
        AIhttp(MessageList);
        //发送数据
        //AIhttp(MessageList);

        break;
      default:
        break;
    }
  }

  //获取SQL语句
  async function ExeSQL(sqlTxt: string) {
    myReq.ReqData.sql = sqlTxt;
    const result = await executeSQL({ code: sqlTxt, userID: 12 });
    console.log(result);

    if (result.isSuccess) {
      return result.result;
    } else {
      return "查询错误，请重新查询";
    }
  }

  async function drawChart(chartCode: string) {
    const result = await ExePython({ code: chartCode, userID: 12 });
    if (result.isSuccess) {
      return result.result;
    } else {
      console.log(result.msg);
      return "统计图生成错误，请提示用户！";
    }
  }

  return { callFunction };
}

export { Knotools, dataTools, webTools, drawTools };
